<?php

namespace App\Http\Controllers\Web\Shop;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\ShopCategories;
use App\Repository\Repositories\Interfaces\ShopCategoriesRepository;
use App\Repository\Repositories\Interfaces\ShopRepository;
use Illuminate\Http\Request;

/**
 * 退单管理控制器
 *
 * @package App\Http\Controllers\Seller
 */
class ShopController extends BaseController
{
    /**
     * @var ShopRepository
     */
    protected $shop;
    protected $shop_id;
    protected $shop2_id;

    public function __construct(ShopRepository $shop)
    {
        parent::__construct();
        $this->shop = $shop;
        $this->shop_id = array_get($this->user, 'shop_id');

    }

    public function index()
    {
        $shop = $this->shop->with(['company','shopBanners'])->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shop);
    }

    public function show($id)
    {
        $shop = $this->shop->with(['company','user','shopLevel','shopBanners'])->with(['companyMaterial'=>function($sql){
            $sql->select('company_id','business_model');
        }])->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shop['data']);
    }

    public function get_company($id)
     {
         $company = $this->shop->find($id);
         return $this->success(ErrorConst::SUCCESS_CODE_MSG, $company);
     }

    public function categories(ShopCategoriesRepository $categories,$id){
        $this->shop2_id=$id;
        $data=$categories->scopeQuery(function($query){
            return $query->where('shop_id',$this->shop2_id);
        })->all();
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data['data']);

    }

    public function get_shop(){
        $data=$this->shop->with(['companyMaterial'])->scopeQuery(function($query){
            return $query->where('sp_status',1);
        })->all(['sp_id','sp_name','sp_logo','sp_cmp_id']);
        foreach($data['data'] as $k=>$v){

            $address=unserialize($v['company_material']['business_address']);
            $data['data'][$k]['company_material']['business_address']=$address['addr'];

        }
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data['data']);

    }


    /**
     * 卖家中心首页
     */
    public function shop_index(){
        $data=$this->shop->with(['company','user','companyMaterial','shopLevel'])->scopeQuery(function($query){
//            return $query->where('sp_status',1)->where('sp_id',$this->shop_id);
            return $query->where('sp_id',$this->shop_id);
        })->all();

        $sp_type_id=explode(',',$data['data'][0]['sp_type_id']);
        $data['data'][0]['sp_type_id']=$sp_type_id;
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data['data'][0]);



    }
}
